import os
import numpy as np
from PIL import Image
from openpyxl import load_workbook





# 加载 Excel 文件
workbook = load_workbook(filename=r'C:\Users\·jiazi\PycharmProjects\pythonProject1\PBLEducationProject\Examples'
                                  r'\wanDan.xlsx')
output_folder = r'C:\Users\·jiazi\PycharmProjects\pythonProject1\PBLEducationProject\output'
# 选择要处理的工作表
worksheet = workbook['Sheet2']

# 获取工作表中的所有图像
images = worksheet._images
image_positions = {}
index = 0
# 遍历图像并打印位置信息
for image in images:
    # 获取图像的左上角和右下角坐标
    row = image.anchor.to.row
    column = image.anchor.to.col
    print(f"Image position: " + f"row: {row}, column: {column}")
    type=image.format
    print("图片的类型是：\t"+type)
    pic_name = f"pic{index}."+type  # 输出图片名称
    index += 1
    data = image.ref
    with open(os.path.join(output_folder, pic_name), 'wb') as img_file:
        img_pil = Image.open(image.ref).convert("RGB")
        img_array = np.array(img_pil)
        # 将 NumPy 数组保存为图像文件
        img_pil.save(os.path.join(output_folder, pic_name))
        # 记录图片位置信息（这里假设仍需要记录）
    image_positions[(row, column)] = pic_name

print(image_positions)
